package com.eliza.demo.db.activity

import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.eliza.demo.R
import com.eliza.demo.db.fg.InfoShowFragment
import com.eliza.demo.db.fg.InsertContactor
import com.eliza.demo.vm.ContactorDbVm
import com.eliza.library.dao.ContactorDao
import com.eliza.library.dao.ContactorDatabase
import com.eliza.library.model.Contactor
import com.eliza.library.tools.InfoTools
import kotlinx.coroutines.launch


/*-*- coding:utf-8 -*-
 * @Author  : debi
 * @Time    : 5/1/22
 * @Software: Android Studio
 */

class ContactorActivity : AppCompatActivity() {

    private var context = this
    lateinit var isc: InsertContactor
    lateinit var isf: InfoShowFragment
    lateinit var contactorDao: ContactorDao
    lateinit var cdv: ContactorDbVm


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_contactor)
        isc = supportFragmentManager.findFragmentById(R.id.Contactor_fg) as InsertContactor
        isf = supportFragmentManager.findFragmentById(R.id.Contactor_fg_recycle) as InfoShowFragment

        contactorDao = ContactorDatabase.getInstance(context).contactorDao

        cdv = ViewModelProvider(
            this, ViewModelProvider.AndroidViewModelFactory(application)
        ).get(ContactorDbVm::class.java)

        isf.setAllContactorsLive(cdv)


        BtnOption()
    }

    fun BtnOption() {
        findViewById<Button>(R.id.Contactor_recycle_mUpdate).setOnClickListener {
            /*协程异步操作数据库*/
            lifecycleScope.launch {
                mUpdate()
            }
        }
        findViewById<Button>(R.id.Contactor_recycle_mDelete).setOnClickListener {
            lifecycleScope.launch {
                mDelete()
            }

        }
        findViewById<Button>(R.id.Contactor_recycle_mQuery).setOnClickListener {
            lifecycleScope.launch {
                mQuery()
            }
        }
        findViewById<Button>(R.id.Contactor_recycle_mInsert).setOnClickListener {
            lifecycleScope.launch {
                mInsert()
            }
        }
    }

    suspend fun mInsert() {
        InfoTools.LogTools(this::class.java.name, "contactorLiveData.value ${isc.getData()}")
        var flag = false
        contactorDao.getAllConList().value?.forEach { v ->
            if (v.id == isc.getData().id) {
                flag = true
            }
        }

        if (!flag) {
            contactorDao.insertCon(
                Contactor(
                    isc.getData().name,
                    isc.getData().age,
                    isc.getData().tel,
                    isc.getData().email
                )
            )
        } else {
            InfoTools.ToastTools(context, "存在Id相同的元素")
        }
    }

    /*
    * 通过 Id 删除
    * */
    suspend fun mDelete() {
        contactorDao.deleteCon(Contactor(isc.getData().id))
    }

    suspend fun mUpdate() {
        contactorDao.updateCon(isc.getData())
    }

    fun mQuery() {
        contactorDao.getAllConList()

    }

}